package com.wgts.wgts.mvp.view.activity.myorder;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.dialog.ToastDate;
import com.wgts.wgts.control.dialog.ToastDialogFragment;
import com.wgts.wgts.control.recycleview.CustomLineaLayoutManager;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerEvaluateActivityComponent;
import com.wgts.wgts.dagger.module.EvaluateActivityModule;
import com.wgts.wgts.mvp.presenter.IEvaluateActivityPresenter;
import com.wgts.wgts.mvp.view.IEvaluateActivityView;
import com.wgts.wgts.mvp.view.activity.pay.ContinuePayActivity;
import com.wgts.wgts.netmodle.resp.EvaluateEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *
 *   data:2018/9/2515:57
 *
 */public class EvaluateActivity extends BaseActivity implements IEvaluateActivityView{
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.evaluate_recycle)
    RecyclerView evaluateRecycle;
    @BindView(R.id.evaluate_button)
    LinearLayout evaluateButton;
    public static String ORDER_ID = "orderId";
    public static final int EVALUATE_RESULT = 333;
    private String orderId;

    private EvaluateAdapter evaluateAdapter;
    private List<EvaluateEntity> entityList;

    @Inject
    IEvaluateActivityPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.evaluate_activity);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        orderId = intent.getStringExtra(ORDER_ID);
        includeTitle.setText("评价商品");
        initView();
    }

    @Override
    public void init() {
        DaggerEvaluateActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .evaluateActivityModule(new EvaluateActivityModule(this))
                .build()
                .inject(this);
        entityList = new ArrayList<>();
    }

    private void initView(){
       evaluateRecycle.setLayoutManager(new LinearLayoutManager(this));

       presenter.initList(orderId);
    }

    @OnClick({R.id.back_btn_ly, R.id.evaluate_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                close();
                break;
            case R.id.evaluate_button:
                presenter.isAllHaveEvaluate();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            close();
        }

        return super.onKeyDown(keyCode, event);
    }

    //返回时的方法
    private void close(){
        ToastDate toastDate = new ToastDate();
        toastDate.setCancelQuestion("当前信息已编辑，是否退出评价");
        toastDate.setCancelText("取消评价");
        toastDate.setPointText("继续评价");
        final ToastDialogFragment toastDialogFragment = ToastDialogFragment.setToastMessage(toastDate);
        toastDialogFragment.show(getSupportFragmentManager(), "EvaluateDialog");
        toastDialogFragment.setOnClickListener(new ToastDialogFragment.OnClickListener() {
            @Override
            public void onClickCancle() {
                toastDialogFragment.dismiss();
            }

            @Override
            public void onClickEnsuer() {
                finish();
            }
        });
    }

    @Override
    public void onError(int status, String msg) {
        dialogDismiss();
        MoHuToast.showToast(msg);
    }

    @Override
    public void initList(List<EvaluateEntity> evaluateEntities) {
        evaluateAdapter = new EvaluateAdapter(this, evaluateEntities, presenter);
        evaluateRecycle.setAdapter(evaluateAdapter);
        //evaluateAdapter.notifyDataSetChanged();
    }

    @Override
    public void checkNotify() {
        evaluateAdapter.checkNotify();
    }

    @Override
    public void haveEvaluate(boolean isAllHaveEvaluate) {
        if (isAllHaveEvaluate){
            showDialog();
            presenter.issueEvaluate(orderId);
        }else {
            MoHuToast.showToast("请选则商品评价");
        }
    }

    @Override
    public void issueEvaluate() {
        MoHuToast.showToast("评价成功");
        dialogDismiss();
        Intent intent = new Intent();
        intent.putExtra("Evaluate", "success");
        setResult(ContinuePayActivity.PAY_RESULT_OK, intent);
        finish();
    }
}
